package com.harry.martin_skill_master.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import com.harry.martin_skill_master.R;
import com.harry.martin_skill_master.util.ActivityCollector;

import java.io.Serializable;

/**
 * @author Martin-harry
 * @date 2021/8/10
 * @address
 * @Desc Activity基类
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    //两次点击按钮的最小间隔，目前为1秒
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime;
    private SparseArray<View> mViews;
    protected RelativeLayout titleBar;
    protected TextView title;
    protected RelativeLayout baseLayout;

    //获取Activity的layout
    public abstract View addContentLayout();

    //初始化控件
    public abstract void initViews();

    //初始化点击事件
    public abstract void initListener();

    //初始化数据(设置数据、获取数据、读取网络数据)
    public abstract void initData();


    //设置同一按钮在1秒内只响应一次点击事件
    public abstract void onSingleClick(View v);

    //设置同一按钮在短时间内可重复响应点击事件
    public abstract void onMultiClick(View v);

    public void onClick(View v) {
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            lastClickTime = curClickTime;

            onSingleClick(v);
        }

        onMultiClick(v);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
        mViews = new SparseArray<>();
        //初始化视图
        setContentView(R.layout.activity_base);
        titleBar = findViewById(R.id.baseTitle);
        title = findViewById(R.id.title);
        baseLayout = findViewById(R.id.baseLayout);
        baseLayout.addView(addContentLayout());
        initViews();
        initListener();
        initData();

        SharedPreferences preferences = getSharedPreferences("appcompat", Context.MODE_PRIVATE);
        //取出保存的值（取数据）
        boolean isChecked = preferences.getBoolean("isChecked", false);
        //根据保存的值设置主题状态
        if (isChecked) {
            getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            //设置夜间模式背景色
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setNavigationBarColor(Color.parseColor("#303030"));
            }
        } else {
            getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            //设置日间模式背景色
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setNavigationBarColor(Color.parseColor("#ffffff"));
            }
        }
    }

    /**
     * 通过ID找到View
     *
     * @param viewId
     * @param <E>
     * @return
     */
    public <E extends View> E findView(int viewId) {
        E view = (E) mViews.get(viewId);
        if (view == null) {
            view = (E) findViewById(viewId);
            mViews.put(viewId, view);
        }
        return view;
    }

    /**
     * View设置OnClick事件
     *
     * @param view
     * @param <E>
     */
    public <E extends View> void setOnClick(E view) {
        view.setOnClickListener(this);
    }

    protected void openActivity(Class<?> cls) {
        openActivity(this, cls);
    }

    public static void openActivity(Context context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
    }

    /**
     * 打开 Activity 的同时传递一个数据
     *
     * @param cls
     * @param key
     * @param value
     * @param <V>
     */
    protected <V extends Serializable> void openActivity(Class<?> cls, String key, V value) {
        openActivity(this, cls, key, value);
    }


    /**
     * 打开 Activity 的同时传递一个数据
     *
     * @param context
     * @param cls
     * @param key
     * @param value
     * @param <V>
     */
    public <V extends Serializable> void openActivity(Context context, Class<?> cls, String key, V value) {
        Intent intent = new Intent(context, cls);
        intent.putExtra(key, value);
        context.startActivity(intent);
    }

    public void setTitle(String titleName) {
        title.setText(titleName);
    }

    public void hideTitle() {
        titleBar.setVisibility(View.GONE);
    }

    public void showTitle() {
        titleBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}
